package com.wits.platform.entity;

import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

@Entity
@Table(name = "wits_resource")
public class Resource extends BaseEntity{
	/**
	 * 
	 */
	private static final long serialVersionUID = 9050209546988972335L;

	public Resource(){
		
	}
	
	private String rscId;
	
	/**
	 * 资源名称
	 */
	private String rscName;

	/**
	 * 资源地址
	 */
	private String rscUri;

	/**
	 * 权限表达式
	 */
	private String rscPerm;
	
	private Menu menu;
	
	
	private List<Role> roles;




	@Id
	@Column(name="rsc_id",length=36)
	public String getRscId() {
		return rscId;
	}



	public void setRscId(String rscId) {
		this.rscId = rscId;
	}


	@Column(name="rsc_name",length=20)
	public String getRscName() {
		return rscName;
	}


	public void setRscName(String rscName) {
		this.rscName = rscName;
	}

	@Column(name="rsc_uri",length=100)
	public String getRscUri() {
		return rscUri;
	}



	public void setRscUri(String rscUri) {
		this.rscUri = rscUri;
	}

	@Column(name="rsc_perm",length=100)
	public String getRscPerm() {
		return rscPerm;
	}



	public void setRscPerm(String rscPerm) {
		this.rscPerm = rscPerm;
	}
	

	@ManyToOne
	@JoinColumn(name = "mu_id")
	public Menu getMenu() {
		return menu;
	}



	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "resources")
	public List<Role> getRoles() {
		return roles;
	}
	

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	private static JsonConfig jsonConfig = new JsonConfig();
	
	@Transient
	public static JsonConfig getJsonConfig() {
		return jsonConfig;
	}

	static {
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			@Override
			public boolean apply(Object arg0, String name, Object arg2) {
				if (name.equals("dateCre") || name.equals("dateUpd") || name.equals("menu") || name.equals("roles")) {
					return true;
				}
				return false;
			}
		});
	}
	
	@Override
	public String display() {
		// TODO Auto-generated method stub
		return this.rscUri;
	}



	@Override
	public String toString() {
		return JSONObject.fromObject(this,jsonConfig).toString();
	}
	
	public String toRightTree(){
		StringBuilder str=new StringBuilder();
		str.append("{\"id\":\"").append(this.rscId)
			.append("\",\"name\":\"").append(this.rscName)
			.append("\",\"type\":\"resource")
		    .append("\",\"pId\":\"").append(this.menu.getMuId())
		    .append("\"}");
		return str.toString();
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((rscId == null) ? 0 : rscId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Resource other = (Resource) obj;
		if (rscId == null) {
			if (other.rscId != null)
				return false;
		} else if (!rscId.equals(other.rscId))
			return false;
		return true;
	}




	

}
